import { observable, action, toJS } from 'mobx';
import { WeaTableNew, WeaForm } from 'comsMobx';
import * as Bctt from '../constants/BaseListConstant';
import { Button } from 'antd';

const { TableStore } = WeaTableNew;
const onlySessionKey = Bctt.ONLY_SESSION_KEY;

class BaseListStore {
    @observable loading = false;
    @observable viewTop = true;
    @observable titleName = '';
    @observable titleIcon = '';
    @observable rightMenu = [];
    @observable showSearchAd = false;
    @observable searchKey = '';
    @observable searchConditionForm = {};
    @observable searchConditionComponents = {};
    @observable tableStore = {};
    @observable routerParams = {};
    @observable searchInputName = Bctt.TAB_SEARCH_INPUT_NAME;
    @observable hasRight = true;
    @observable verified = false;
    @observable storeVisible = false;

    getTableData(requestParams, callBackFn) {

    }

    reLoad = () => {
      this.tableStore.getDatas(this.tableStore.dataKey, this.tableStore.current, this.tableStore.pageSize, this.tableStore.sortParams);
    }

    @action
    componentWillMount=(componentParams) => {
      this.routerParams = componentParams;
      this.searchConditionForm = new WeaForm();
      this.tableStore = new TableStore({ dataHandle: (datas) => {
        datas.forEach((data) => {
          if (typeof (data.subListspan) === 'string') {
            data.subListspan = JSON.parse(data.subListspan);
          }
        });
        return datas;
      } });
    }

    @action
    componentDidMount=() => {
      this.loading = true;
      const requestParams = {};
      requestParams[onlySessionKey] = false;
      this.getTableData(requestParams, this.componentInitCallBack);
    }

    @action
    componentWillUnmount=() => {
      this.loading = false;
      this.viewTop = true;
      this.titleName = '';
      this.titleIcon = '';
      this.rightMenu = [];
      this.showSearchAd = false;
      this.searchKey = '';
      this.tableStore = {};
      this.routerParams = {};
      this.searchConditionForm = {};
      this.searchConditionComponents = {};
      this.hasRight = true;
      this.verified = false;
      this.storeVisible = false;
    }

    @action
    doSearch=(params = {}) => {
      this.loading = true;
      this.setShowSearchAd(false);
      const initParams = {};
      initParams[onlySessionKey] = true;
      initParams[this.searchInputName] = this.searchKey;
      const searchParamsAd = this.searchConditionForm.getFormParams();
      const requestParams = { ...initParams, ...searchParamsAd, ...params };
      for (const item in requestParams) {
        if (requestParams[item] == '') {
          delete requestParams[item];
        }
      }
      this.getTableData(requestParams, this.doSearchCallBack);
    }

    @action
    componentInitCallBack=(data = {}) => {
      this.verified = true;
      if (data.hasright) {
        const content = data.datas.content;
        this.titleName = content.titleName;
        this.titleIcon = content.titleIcon;
        this.rightMenu = content.rightMenu;
        this.searchConditionComponents = content.searchConditionComponents;
        // 初始化高级搜索
        this.searchConditionForm.initFormFields(content.searchConditionComponents);
        // 初始化表格数据
        this.tableStore.getDatas(content.sessionKey, 1);
        this.loading = false;
      } else {
        this.hasRight = false;
      }
    }

    @action
    doSearchCallBack=(data = {}) => {
      const content = data.datas.content;
      this.tableStore.getDatas(content.sessionKey, 1);
      this.loading = false;
    }

    getRightMenus() {
      const { rightMenu, tableStore } = this;
      const btnArr = [];
      rightMenu && rightMenu.length > 0 && rightMenu.map((m) => {
        btnArr.push({
          key: m.type,
          icon: <i className={m.menuIcon} />,
          content: m.menuName,
          onClick: (key) => {
            if (key == Bctt.BTN_EXPORT) {
              this.excelExport(this.excelExportCallBack);
            } else if (key == Bctt.BTN_SEARCH) {
              this.doSearch();
            } else if (key == Bctt.BTN_COLUMN) {
              tableStore.setColSetVisible(true);
              tableStore.tableColSet(true);
            } else if (key == Bctt.BTN_STORE) {
              this.showStoreDialog();
            } else {
              this.rightMenuClick(key);
            }
          },
        });
      });
      return btnArr;
    }

    @action
    showStoreDialog() {
      this.storeVisible = true;
    }

    @action
    hideStoreDialog() {
      this.storeVisible = false;
    }

    getTopButtons() {
      const { rightMenu, tableStore } = this;
      const btnArr = [];
      rightMenu && rightMenu.length > 0 && rightMenu.map((m) => {
        m.isTop == '1' && btnArr.length < 4 && btnArr.push(
          <Button
            ecId={`${this && this.props && this.props.ecId || ''}_Button@thajsv@${m.type}`}
            type="primary"
            disabled={m.isControl == '1' && toJS(tableStore.selectedRowKeys).length == 0}
            onClick={() => {
              const fn = m.menuFun.indexOf('this') >= 0 ? `${m.menuFun.split('this')[0]})` : m.menuFun;
              if (m.type == Bctt.BTN_EXPORT) {
                this.excelExport(this.excelExportCallBack);
              } else {
                this.rightMenuClick(m.type);
              }
            }}
          >
            {m.menuName}
          </Button>,
        );
      });
      const btnArrCustom = this.getTopButtonsCustom();
      return btnArrCustom.concat(btnArr);
    }

    getTopButtonsCustom() {
      const btnArr = [];
      return btnArr;
    }

    excelExportCallBack(data) {
      window.location = Bctt.ADDR_EXCEL_EXPORT;
    }

    rightMenuClick(key) {

    }

    @action
    setShowSearchAd=(bool = false) => {
      this.showSearchAd = bool;
    }

    @action
    setSearchKey=(v = '') => {
      this.searchKey = v;
      try {
        const searchKeyField = {};
        searchKeyField[this.searchInputName] = { value: v };
        this.searchConditionForm.updateFields(searchKeyField, false);
      } catch (error) {
        // 高级搜索中没有searchKey的情况
      }
    }

    @action
    searchConditionFieldChange=(fieldObj) => {
      if (fieldObj[this.searchInputName]) {
        this.searchKey = fieldObj[this.searchInputName].value;
      }
    }

    @action
    clearAdFields=() => {
      this.searchConditionForm.reset();
      this.searchKey = '';
    }

    excelExport(excelExportCallBack) {

    }
}

export default BaseListStore;
